<template>
  <div class="container">
    <div class="post-detail-layout" v-loading="loading" element-loading-background="rgba(255, 255, 255, 0.7)">
      <template v-if="!loading">
        <article class="post-detail" v-if="post">
          <router-link to="/posts" class="back-button">
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
              <path d="M19 12H5"></path>
              <path d="M12 19l-7-7 7-7"></path>
            </svg>
            返回文章列表
          </router-link>
          <div class="post-header">
            <h1 class="post-title">{{ post.title }}</h1>
            <div class="post-meta">
              <div class="post-info">
                <span class="post-date">{{ post.createdTime }}</span>
                <span class="post-views">{{ post.views }} views</span>
              </div>
              <div class="post-actions">
                <router-link :to="`/posts/${post.id}/edit`" class="edit-button">
                  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none"
                    stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                    <path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"></path>
                    <path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"></path>
                  </svg>
                  编辑
                </router-link>
                <div class="post-tags">
                  <span v-for="tag in post.tags" :key="tag" class="post-tag">{{ tag }}</span>
                </div>
              </div>
            </div>
          </div>

          <div class="post-content">
            <MarkdownRenderer :content="post.content" />
          </div>

          <CommentSection :post-id="post.id" />

          <div class="post-footer">
            <div class="post-author">
              <img src="../../public/logo.jpg" alt="grtsinry43" class="author-avatar" />
              <div class="author-info">
                <h3 class="author-name">lcp</h3>
                <p class="author-bio">Java开发者</p>
              </div>
            </div>

            <div class="post-navigation">
              <router-link v-if="prevPost" :to="`/posts/${prevPost.id}`" class="post-nav-link prev">
                <span class="post-nav-label">上一篇</span>
                <span class="post-nav-title">{{ prevPost.title }}</span>
              </router-link>
              <router-link v-if="nextPost" :to="`/posts/${nextPost.id}`" class="post-nav-link next">
                <span class="post-nav-label">下一篇</span>
                <span class="post-nav-title">{{ nextPost.title }}</span>
              </router-link>
            </div>
          </div>
        </article>
        <aside class="post-sidebar" v-if="post">
          <TableOfContents contentSelector=".post-content" />
        </aside>
      </template>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import CommentSection from '../components/CommentSection.vue'
import MarkdownRenderer from '../components/MarkdownRenderer.vue'
import TableOfContents from '../components/TableOfContents.vue'
import { getPostById } from '../api/post.js'

const route = useRoute()
const router = useRouter()
const loading = ref(true)
const post = ref(null)
const prevPost = ref(null)
const nextPost = ref(null)

const fetchPostData = async (postId) => {
  loading.value = true
  try {
    const res = await getPostById(postId)
    if (res.data && res.data.length > 0) {
      // 在数组中找到当前文章的索引
      const currentIndex = res.data.findIndex(p => p.id == postId)
      
      if (currentIndex !== -1) {
        post.value = res.data[currentIndex]
        prevPost.value = currentIndex > 0 ? res.data[currentIndex - 1] : null
        nextPost.value = currentIndex < res.data.length - 1 ? res.data[currentIndex + 1] : null
      } else {
        router.push('/not-found')
      }
    } else {
      router.push('/not-found')
    }
  } catch (error) {
    console.error('Failed to load post:', error)
    router.push('/not-found')
  } finally {
    loading.value = false
  }
}

// 监听路由参数变化
watch(
  () => route.params.id,
  (newId) => {
    if (newId) {
      fetchPostData(newId)
    }
  }
)

onMounted(() => {
  fetchPostData(route.params.id)
})
</script>

<style scoped>
.post-detail-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: 2rem;
  margin: 0 auto;
  max-width: 1200px; /* Add a max-width for better responsiveness */
}

.post-detail {
  max-width: 800px;
  padding: 2rem 0;
  margin: 0 auto; /* Center content on smaller screens */
}

.back-button {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
  padding: 0.5rem 0.75rem;
  background-color: var(--color-hover);
  border-radius: 4px;
  color: var(--color-text);
  font-size: 0.875rem;
  width: fit-content;
  transition: all 0.2s;
}

.back-button:hover {
  background-color: var(--color-primary);
  color: white;
  text-decoration: none;
  transform: translateX(-3px);
}

.post-header {
  margin-bottom: 3rem;
}

.post-title {
  font-size: 2.5rem;
  line-height: 1.2;
  margin-bottom: 1rem;
}

.post-meta {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 1rem;
}

.post-info {
  display: flex;
  gap: 1rem;
  color: var(--color-text-light);
}

.post-actions {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.75rem;
}

.edit-button {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem 0.75rem;
  background-color: var(--color-hover);
  border-radius: 4px;
  color: var(--color-text);
  font-size: 0.875rem;
  transition: all 0.2s;
}

.edit-button:hover {
  background-color: var(--color-primary);
  color: white;
  text-decoration: none;
}

.post-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.post-tag {
  padding: 0.25rem 0.5rem;
  background-color: var(--color-hover);
  border-radius: 4px;
  font-size: 0.75rem;
  color: var(--color-primary);
}

.post-content {
  margin-bottom: 3rem;
}

.post-footer {
  margin-top: 3rem;
  padding-top: 2rem;
  border-top: 1px solid var(--color-border);
}

.post-author {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 2rem;
}

.author-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  object-fit: cover;
}

.author-name {
  font-size: 1.25rem;
  font-weight: 600;
  margin-bottom: 0.25rem;
}

.author-bio {
  color: var(--color-text-light);
  font-size: 0.875rem;
}

.post-navigation {
  display: flex;
  justify-content: space-between;
}

.post-nav-link {
  display: flex;
  flex-direction: column;
  max-width: 45%;
}

.post-nav-link.prev {
  align-items: flex-start;
}

.post-nav-link.next {
  align-items: flex-end;
}

.post-nav-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  color: var(--color-text-light);
  margin-bottom: 0.25rem;
}

.post-nav-title {
  font-size: 0.9rem;
  font-weight: 500;
}

.post-sidebar {
  padding-top: 2rem;
}

.post-loading {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
}

.post-skeleton {
  min-height: 70vh;
}

.skeleton-title {
  height: 2.5rem;
  background: #f0f0f0;
  margin-bottom: 1.5rem;
  border-radius: 4px;
  width: 80%;
}

.skeleton-meta {
  height: 1rem;
  background: #f0f0f0;
  margin-bottom: 2rem;
  border-radius: 4px;
  width: 40%;
}

.skeleton-content {
  height: 400px;
  background: #f0f0f0;
  border-radius: 4px;
}

.post-not-found {
  text-align: center;
  padding: 2rem;
}

.back-link {
  display: inline-block;
  margin-top: 1rem;
  color: var(--color-primary);
  text-decoration: underline;
}

/* 响应式布局 */
@media (max-width: 1024px) {
  .post-detail-layout {
    grid-template-columns: 1fr;
    gap: 1.5rem; /* Reduce gap for smaller screens */
  }

  .post-sidebar {
    padding-top: 0;
    padding-bottom: 2rem;
  }
}

@media (max-width: 768px) {
  .post-detail-layout {
    grid-template-columns: 1fr; /* Single column layout */
    padding: 1rem; /* Add padding for smaller screens */
  }

  .post-sidebar {
    padding: 1rem; /* Add padding for the sidebar */
    background-color: var(--color-card); /* Add a background for better visibility */
    border-radius: var(--radius-md); /* Add rounded corners */
  }

  .post-detail {
    margin: 0 auto; /* Center the content */
  }

  .comment-section {
    padding: 1rem; /* Add padding for comments */
    background-color: var(--color-card); /* Add a background for better visibility */
    border-radius: var(--radius-md); /* Add rounded corners */
  }

  .post-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 1rem;
  }

  .post-actions {
    width: 100%;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }

  .post-tags {
    flex: 1;
    justify-content: flex-end;
  }

  .post-detail-layout {
    padding: 1rem; /* Add padding for smaller screens */
  }

  .post-sidebar {
    padding: 1rem; /* Add padding for the sidebar */
    background-color: var(--color-card); /* Add a background for better visibility */
    border-radius: var(--radius-md); /* Add rounded corners */
  }
}
</style>
